﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using Common.Tools;
using Domain;
using mshtml;
using Spider.Service;

namespace Spider.Spiders
{
    /// <summary>
    /// 通用爬虫
    /// </summary>
    internal abstract class Spider
    {
        
        /// <summary>
        /// WebBrowser组件
        /// </summary>
        public static WebBrowser WebBrowser { get; set; }
        /// <summary>
        /// WebBrowser组件访问延时秒
        /// </summary>
        public static int WebBrowserDelay { get; set; }
        /// <summary>
        /// 开始工作
        /// </summary>
        /// <param name="data"></param>
        public abstract void StartToWork(ToDoData data);
        /// <summary>
        /// 获取最终的请求Url地址
        /// </summary>
        /// <param name="sourceContent"></param>
        /// <returns></returns>
        protected abstract string GetFinalUrl(string sourceContent, PageDataSource dataSource);
        /// <summary>
        /// 获取目标地址网页的源代码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetSourceHtml(string url,Encoding code)
        {
            WebClient request = new WebClient();
            if(code!=null)
                request.Encoding = code;
            return request.DownloadString(url);
        }
        /// <summary>
        /// 通过浏览器组件获取最终的HTML
        /// </summary>
        /// <param name="url"></param>
        public string GetFinalHtmlByWebBrowser(string url)
        {
            if (string.IsNullOrWhiteSpace(url)) return null;
            WebBrowser.Navigate(url);
            DateTime timestart = DateTime.Now;
            //先要等待主页加载完毕
            var comp = false;
            WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler((sender, e) =>
            {
                comp = true;
            });
            while (!comp)
            {
                if (DateTime.Now - timestart > new TimeSpan(0, 0, 0, 30))//最大超时30秒
                {
                    CustomsLog.WriteInfoLog("访问URL超时","Url:"+url);
                    return null;
                }
                Application.DoEvents();
            }
            while (WebBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                if (DateTime.Now - timestart > new TimeSpan(0, 0, 0, 30))//最大超时30秒
                {
                    CustomsLog.WriteInfoLog("访问URL超时", "Url:" + url);
                    return null;
                }
                Application.DoEvents();
            }
            SpiderHelper.DelayTime((WebBrowserDelay == 0 ? 10 : WebBrowserDelay));
            var htmldocument = (mshtml.HTMLDocument)WebBrowser.Document.DomDocument;
            return htmldocument.documentElement.outerHTML; ;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetWebContentWithCookie(string url, CookieContainer cookie)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.CookieContainer = cookie;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        /// <summary>
        /// 提取网页中的链接
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public string[] GetLink(string strHtml)
        {

            string RegexStr = "(?<=href[\\s\\r]*=[\\s\\r]*[\"\'])([^\"\'\\s\\r>]*)(?<=[\"\'\\s\\r]{0,1})";

            Regex reg = new Regex(RegexStr, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(strHtml);
            if (mc.Count > 0)
            {
                string[] strHref = new string[mc.Count];
                int i = 0;
                foreach (Match m in mc)
                {
                    strHref[i] = m.Groups[1].Value;
                    ++i;
                }
                return strHref;
            }
            return null;
        }
        /// <summary>
        /// 分析链接所属平台类型
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static PageDataSource GetPageDataSource(string url)
        {
            if (url.StartsWith("http://g.cnc.qzone.qq.com"))
            {
                return PageDataSource.QQVisitor;
            }
            else if (url.Contains("ygdy8.net"))
            {
                return PageDataSource.Ygdy8netIndexPage;
            }
            else
            {
                return PageDataSource.Unknown;
            }
        }
        /// <summary>
        /// 分析ToDo内容类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PageDataSource GetContentDataSource(string type)
        {
            switch (type)
            {
                case "qqforzone":
                    return PageDataSource.QQNumberForZone;
                    break;
                case "qqforwb":
                    return PageDataSource.QQNumberForWb;
                    break;
                case "qqSeed":
                    return PageDataSource.QQSeed;
                    break;
                default: return PageDataSource.Unknown;
            }
        }

    }
}
